﻿Imports Microsoft.Xna.Framework.Content
Imports Runa.Resources.ContentReader.VectorFont
Imports Microsoft.Xna.Framework

Namespace Global.Runa.Resources.ContentReader

    Public Class VectorFontReader
        Inherits ContentTypeReader(Of VectorFont.VectorFont)

        ''' <summary>Load a vector font from a stored XNA asset</summary>
        ''' <param name="input">Reader from which the asset can be read</param>
        ''' <param name="existingInstance">Optional existing instance we are reloading</param>
        ''' <returns>The loaded VectorFont instance</returns>
        Protected Overrides Function Read(input As Content.ContentReader, existingInstance As VectorFont.VectorFont) As VectorFont.VectorFont
            Dim lineHeight As Single = input.ReadSingle()

            ' Which index corresponds to which character
            Dim characterMap As Dictionary(Of Char, Integer) = input.ReadObject(Of Dictionary(Of Char, Integer))()

            ' Vectors of each character
            Dim characters As List(Of VectorFontCharacter) = input.ReadObject(Of List(Of VectorFontCharacter))()

            ' Special distance adjustments between some characters
            Dim kerningTable As New Dictionary(Of VectorFont.VectorFont.KerningPair, Vector2)()

            Dim kerningEntryCount As Integer = input.ReadInt32()
            For index As Integer = 0 To kerningEntryCount - 1
                Dim left As Char = input.ReadChar()
                Dim right As Char = input.ReadChar()
                Dim kerning As Vector2 = input.ReadVector2()

                kerningTable.Add(New VectorFont.VectorFont.KerningPair(left, right), kerning)
            Next

            Return New VectorFont.VectorFont(lineHeight, characters, characterMap, kerningTable)
        End Function

    End Class

End Namespace